package cn.zy.amyan.client.handler;

import cn.zy.amyan.client.future.AyFutureResponse;
import cn.zy.amyan.core.base.ResponseData;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.logging.Logger;

/**
 * @author 子言
 * @date 2024/5/1 下午 16:41
 */
public class ClientHandler extends SimpleChannelInboundHandler<ResponseData> {
	private static final Logger log = Logger.getLogger("appLogger");
	@Override
	protected void channelRead0(ChannelHandlerContext ctx, ResponseData msg) throws Exception {
		log.info("接收到服务端的消息：requestId：" + msg.getRequestId());
		// 接收到服务端的消息后，通知响应已经完成
		// 通过RpcFutureResponse中的Map获取到RpcFutureResponse对象，进⾏⽅法回调
		AyFutureResponse rpcFutureResponse = AyFutureResponse.getAyFutureResponse(msg.getRequestId());
		if(null != rpcFutureResponse){
			rpcFutureResponse.setResponse(msg);
		}else{
			log.severe("没有找到对应的RpcFutureResponse对象~ requestId = " + msg.getRequestId());
		}
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
			throws Exception {
		log.severe("客户端出错~ ");
		ctx.close();
	}
}